﻿// -----------------------------------------------------------------------
// <copyright file="AdornmentGrid.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TouchControls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
using System.Windows;
    using System.Windows.Media;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AdornmentGrid : Grid
    {
        public Canvas Adornment { get; set; }

        public override void EndInit()
        {
            base.EndInit();
            this.InitAdornment();
        }

        public void InitAdornment()
        {
            if (this.Adornment == null)
            {
                this.Adornment = new Canvas();
                //Adornment.Background = new SolidColorBrush(Colors.Transparent);
                this.Children.Add(this.Adornment);
            }
            else
            {
                this.MoveAdornmentToBottom();
            }
        }

        private void MoveAdornmentToBottom()
        {
            if (this.Children.IndexOf(this.Adornment) != this.Children.Count - 1)
            {
                this.Children.Remove(this.Adornment);
                this.Children.Add(this.Adornment);
            }
        }

        protected override Size MeasureOverride(Size constraint)
        {
            this.MoveAdornmentToBottom();
            return base.MeasureOverride(constraint);
        }

        public static AdornmentGrid For(DependencyObject obj)
        {
            return VisualTreeHelpers.FindParent<AdornmentGrid>(obj);
        }
    }
}
